package com.zhuoya.healthcard;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MyCardNoView extends LinearLayout {
    List<String> inputNumbers = new ArrayList<>();
    private List<TextView> tvList = new ArrayList<>();

    private static final int BLANK_NUMBER = 18;


    public MyCardNoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        for (int i = 0; i < BLANK_NUMBER; i++) {
            TextView textView = new TextView(context);
            LinearLayout.LayoutParams layoutParams = new LayoutParams(dp2px(context, 25), dp2px(context, 35));
            layoutParams.setLayoutDirection(HORIZONTAL);
            layoutParams.setMarginStart(dp2px(context, 6));
            textView.setLayoutParams(layoutParams);
            textView.setTextColor(Color.parseColor("#FFFFFF"));
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 24);
            textView.setGravity(Gravity.CENTER);
            textView.getPaint().setFakeBoldText(true);
            textView.setBackground(context.getResources().getDrawable(R.drawable.blank_shape));
            addView(textView);
            tvList.add(textView);
        }
    }


    private static int dp2px(Context context, int dp) {
        return (int) (context.getResources().getDisplayMetrics().density * dp + 0.5);
    }


    public void deleteNum() {
        if (inputNumbers.size() > 0) {
            int index = inputNumbers.size() - 1;
            inputNumbers.remove(index);
            tvList.get(index).setText("");
        }

    }

    public void inputNum(String number) {
        if (inputNumbers.size() < BLANK_NUMBER) {
            inputNumbers.add(number);
            tvList.get(inputNumbers.size() - 1).setText(number);
        }

    }


    /**
     * 获取已经输入的内容
     *
     * @return
     */
    public String getContent() {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < inputNumbers.size(); i++) {
            stringBuilder.append(inputNumbers.get(i));
        }

        return stringBuilder.toString();
    }

}
